const { Service } = require('egg');
class AdService extends Service {
  // 新增
  async create(params) {
    try {
      const { ctx } = this;
      const result = await ctx.model.Ad.create(params);
      return result;
    } catch (error) {
      return null;
    }
  }
  // 分页
  async page({ pageIndex, pageSize }) {
    try {
      const { ctx, app } = this;
      const result = await ctx.model.Ad.findAndCountAll({
        order: [['created_at', 'DESC']],
        limit: parseInt(pageSize), // 每页多少条
        offset: parseInt(pageSize) * (parseInt(pageIndex) - 1), // 跳过多少条
        where:{ flag:1}
      });
      return result;
    } catch (error) {
      return null;
    }
  }
  //  获取
  async info(payload) {
    try {
      const { ctx } = this;
      const result = await ctx.model.Ad.findOne({
        where: {
          ...payload,
          flag:1
        },
      });
      return result;
    } catch (error) {
      return null;
    }
  }
  // 修改
  async edit(data) {
    let { adId } = data;
    try {
      const { ctx } = this;
      const result = await ctx.model.Ad.update(
        { ...data },
        {
          where: {
            adId,
          },
        }
      );
      return result;
    } catch (error) {
      return null;
    }
  }
  // 删除
  async del({adId}) {
    try {
      const { ctx } = this;
      const result = await ctx.model.Ad.update(
        { flag: 0 },
        {
          where: {
            adId,
          },
        }
      );
      return result;
    } catch (error) {
      return null;
    }
  }
}
module.exports = AdService;
